package com.tianshi.pxx.controller.admin;


import com.tianshi.pxx.pojo.entity.Product;
import com.tianshi.pxx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    //查询所有商品
    @GetMapping("/all")
    public ResponseEntity<List<Product>> getAllProducts(){
        System.out.println("查询所有商品:");
        List<Product> products = productService.getAll();
        System.out.println(products);
        return ResponseEntity.ok(products);
    }
    //模糊查询商品
    @GetMapping("/search")
    public List<Product> searchProducts(String keyword){
        System.out.println("模糊查询的关键字为:"+keyword);
        return productService.search(keyword);
    }
    //删除商品
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteProduct(@PathVariable("id") Integer id) {
        System.out.println("删除商品:" + id);
        productService.delete(id);
        return ResponseEntity.ok("删除成功");
    }
    //通过价格区间查询商品
    @GetMapping("/searchByPrice")
    public ResponseEntity<List<Product>> searchProductsByPrice(Double minPrice, Double maxPrice){
        System.out.println("查询价格在"+minPrice+"到"+maxPrice+"之间的商品");
        List<Product> products = productService.searchProductsByPrice(minPrice,maxPrice);
        System.out.println(products);
        return ResponseEntity.ok(products);
    }

    //编辑商品
    @PutMapping
    public void editProduct(@RequestBody Product product){
        System.out.println("编辑后的商品信息为:"+product);
        productService.updateProduct(product);
    }
    //新增商品
    @PostMapping
    public String addProduct(@RequestBody Product product) {
        productService.addProduct(product);
        return "success";
    }
}
